﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using ZhouYu.NewWorld.BLL.IService;
using ZhouYu.NewWorld.Model;
using ZhouYu.NewWorld.ViewModel;
using ZhouYu.NewWorld.Web.Models;

namespace ZhouYu.NewWorld.Web.Controllers
{
    public class OrderInfoController : Controller
    {
        //注入
        private IOrderInfoService _orderInfoService;
        public OrderInfoController(IOrderInfoService orderInfoService)
        {
            _orderInfoService = orderInfoService;
        }


        /// <summary>
        /// 订单列表查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public JsonResult GetOrderList(int page, int limit)
        {
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            //定义表数据总数变量
            int count = 0;
            Expression<Func<OrderInfo, bool>> lambdaCategory = a => true;
            Expression<Func<OrderInfo, int>> orderCategory = a => a.Order_Id;
            //创建List集合接收业务层返回值
            List<OrderInfo> userInfos = _orderInfoService.QueryPageList(page, limit, lambdaCategory, orderCategory, out count);
            //赋值
            responseResult.code = 0;
            responseResult.msg = string.Empty;
            responseResult.data = userInfos;
            responseResult.count = count;
            return Json(responseResult, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 修改订单状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult UpdateOrderState(int id, short states)
        {
            Expression<Func<OrderInfo, bool>> lambdaCategory = a => a.Order_Id == id;
            OrderInfo order = _orderInfoService.Query(lambdaCategory);
            order.Order_Status = states;
            order.ModifiedTime = DateTime.Now;
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            responseResult.Success = _orderInfoService.Update(order);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult DelOrder(OrderInfo orderInfo)
        {
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            responseResult.Success = _orderInfoService.Delete(orderInfo);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }


        /// <summary>
        /// 编辑订单
        /// </summary>
        /// <param name="orderInfo"></param>
        /// <returns></returns>
        public JsonResult EditOrder(OrderInfo orderInfo)
        {
            //定义查询表达式
            Expression<Func<OrderInfo, bool>> lambdaCategory = a => a.Order_Id == orderInfo.Order_Id;
            //获取需要修改的对象
            OrderInfo order = _orderInfoService.Query(lambdaCategory);
            order.ModifiedTime = DateTime.Now;
            order.Order_Money = orderInfo.Order_Money;
            order.Order_ProInfo = orderInfo.Order_ProInfo;
            order.Order_Status = orderInfo.Order_Status;
            order.Order_Content = orderInfo.Order_Content;
            order.Order_Type = orderInfo.Order_Type;
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            //修改订单
            responseResult.Success = _orderInfoService.Update(order);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }
    }
}